package com.jxzyk.model.course;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.core.entity.I_List;
import com.core.entity.params.MapParams;
import com.core.entity.params.ReqParams;
import com.core.photo.HackyViewPager;
import com.core.ui.activity.BaseMainActivity;
import com.core.util.T;
import com.jxzyk.R;
import com.jxzyk.adapter.OutlineAdapter;
import com.jxzyk.config.API;
import com.jxzyk.entity.Course;
import com.jxzyk.entity.User;
import com.jxzyk.entity.tree.Material;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.CollectionRequest;
import com.jxzyk.http.req.CourseRequest;
import com.jxzyk.model.notes.NotesActivity;
import com.jxzyk.util.UserManger;
import com.jxzyk.view.PagerSlidingTabStrip;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.zhy.tree.bean.Node;

@ContentView(R.layout.act_course)
public class CourseActivity extends BaseMainActivity {
	@ViewInject(R.id.viewPager)
	HackyViewPager viewPager;
	@ViewInject(R.id.vPager)
	ViewPager vPager;
	@ViewInject(R.id.tv_course_name)
	TextView tv_name;
	Fragment outlineFragment, detailsFragment;
	@ViewInject(R.id.tabs)
	PagerSlidingTabStrip tabs;
	DisplayMetrics dm;
	String[] titles = { "课程大纲", "课程详情" };
	// private List<FileBean> mDatas = new ArrayList<FileBean>();
	// @ViewInject(R.id.id_tree)
	// private ListView mTree;
	@ViewInject(R.id.cb_course)
	CheckBox checkBox;
	// private TreeListViewAdapter mAdapter;
	private Course course;
	// private List<I_List> lists;
	public Map<Integer, List<I_List>> vaperMap = new HashMap<Integer, List<I_List>>();

	private String Outline_id;
	private String Outline_name;
	private UserManger manger;

	@OnClick(value = { R.id.tv_course_notes })
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_course_notes:
			Bundle bundle = new Bundle();
			String outline_id = getOutline_id();
			String outline_name = getOutline_name();
			if (TextUtils.isEmpty(outline_id)
					|| TextUtils.isEmpty(outline_name)) {
				T.toast("请先选择大纲目录");
				return;
			}
			bundle.putString("outline_id", outline_id);
			bundle.putString("outline_name", outline_name);
			showActivity(NotesActivity.class, bundle);
			break;

		default:
			break;
		}
	}

	@Override
	public void initData(Bundle bundle) {
		dm = getResources().getDisplayMetrics();
		manger = new UserManger(getBaseContext());
		bundle = getIntent().getExtras();
		if (bundle != null) {
			course = (Course) bundle.getSerializable(Course.class.getName());
			if (course != null) {
				try {
					recordCourse();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		setBundle(bundle);
	}

	public void recordCourse() throws Exception {
		CourseRequest request = new CourseRequest(CourseActivity.this,
				API.Course.record);
		User user = manger.getUser();
		ReqParams params = new MapParams();
		params.put("user_id", user.getUser_id());
		params.put("course_id", course.getCourse_id());
		request.setParams(params);
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onFailure(String msg) {
				// TODO Auto-generated method stub

			}
		});
		request.start();
	}

	@Override
	public void initUi() {
		tv_name.setText(course.getCourse_name());
		List<I_List> materials = new ArrayList<>();
		Material material = new Material();
		material.setMaterial_type("图片");
		material.setMaterial_content(course.getCourse_img());
		materials.add(material);
		OutlineAdapter adapter = new OutlineAdapter(this);
		adapter.setContext(getBaseContext());
		adapter.setLists(materials);
		viewPager.setAdapter(adapter);

		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				try {
					collection(isChecked);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		vPager.setAdapter(new MyAdapter(getSupportFragmentManager(), titles));
		tabs.setViewPager(vPager);
	}

	private void collection(boolean isChecked) throws Exception {
		String url = "";
		if (isChecked) {
			url = API.Collection.mark;
		} else {
			url = API.Collection.delMark;
		}
		User user = new UserManger(getBaseContext()).getUser();
		CollectionRequest request = new CollectionRequest(CourseActivity.this,
				url);
		request.setParams(user.getUser_id(), course.getCourse_id());
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				T.toast(json.getString("message"));
			}

			@Override
			public void onFailure(String msg) {

			}
		});
		request.show();
	}

	public void initViewpager(Node node, int id) {
		if (node.isRoot()) {// 根节点
			return;
		}
		List<I_List> materials = vaperMap.get(id);
		// if (node.isLeaf()) {// 子节点
		// T.toast("子节点:" + node.getName() + "|" + "|" + id);
		// } else {
		// T.toast("二级节点:" + node.getName() + "|" + id);
		setOutline_id(null);
		setOutline_name(null);
		if (materials != null) {
			if (materials.size() > 0) {
				Material material = (Material) materials.get(0);
				if (material != null) {
					setOutline_id(material.getOutline_id());
					setOutline_name(node.getName());
				}
			}
			OutlineAdapter adapter = new OutlineAdapter(this);
			adapter.setContext(getBaseContext());
			adapter.setLists(materials);
			viewPager.setAdapter(adapter);
		}
	}

	public class MyAdapter extends FragmentPagerAdapter {
		String[] _titles;

		public MyAdapter(FragmentManager fm, String[] titles) {
			super(fm);
			_titles = titles;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return _titles[position];
		}

		@Override
		public int getCount() {
			return _titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				if (outlineFragment == null) {
					outlineFragment = new OutlineFragment();
				}
				return outlineFragment;
			case 1:
				if (detailsFragment == null) {
					detailsFragment = new DetailsFragment();
				}
				return detailsFragment;
			default:
				return null;
			}
		}
	}

	public String getOutline_name() {
		return Outline_name;
	}

	public void setOutline_name(String outline_name) {
		Outline_name = outline_name;
	}

	public String getOutline_id() {
		return Outline_id;
	}

	public void setOutline_id(String outline_id) {
		Outline_id = outline_id;
	}
}
